﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XZen.Blogger;
using XZen.Cdn.Client.Blobs;

namespace XZen.Blogger
{
    public enum JSModelStatus
    {
        S = 1, N = 2, U = 4, D = 8
    }
 
    public class Attributer
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Title { get; set; }
        public string Category { get; set; }
        public string CoverPageUrl { get; set; }
        public string Body { get; set; }
        public string User { get; set; }
        public string IP { get; set; }
        public string Source { get; set; }
        public string Uri { get; set; }
        public string CreatedOn { get; set; }
        public string UpdatedOn { get; set; }
        public string Attributes { get; set; }

        private string className;

        public string ClassName
        {
            get
            {
                return string.IsNullOrEmpty(className) ?
                    this.GetType().Name : className;
            }
            set { className = value; }
        }

        public Dictionary<string, string> ToAttributes()
        {
            var attrs = new Dictionary<string, string>() { 
                        { "ip" ,IP} , 
                        { "category", Category},
                        { "title", Title},
                        { "author", User},
                        { "description", Body},
                        { "source", Source},
                        { "site", Uri},
                        { "type", ClassName},
                        { "created", CreatedOn},
                        { "updated", UpdatedOn},
                        { "attr", Attributes},
                        { "cover", CoverPageUrl}
            };
            return attrs;
        }
        public void FillAttributes(IDictionary<string, string> attrs)
        {
            IP = attrs.GetStringValue("ip");
            Category = attrs.GetStringValue("category");
            Title = attrs.GetStringValue("title");
            User = attrs.GetStringValue("author");
            Body = attrs.GetStringValue("description");
            Source = attrs.GetStringValue("source");
            Uri = attrs.GetStringValue("site");
            ClassName = attrs.GetStringValue("type");
            CreatedOn = attrs.GetStringValue("created");
            UpdatedOn = attrs.GetStringValue("updated");
            Attributes = attrs.GetStringValue("attr");
            CoverPageUrl = attrs.GetStringValue("cover");
        }
        private string _status = JSModelStatus.N.ToString();
        public string DataStatus
        {
            get { return _status; }
            set { _status = value; isDirty = true; }
        }
        private bool isDirty = false;

        public bool IsDirty
        {
            get { return isDirty; }
            set { isDirty = value; LastModified = DateTime.Now; }
        }

        public DateTime LastModified { get; set; }
    }
    public class Magazine : Attributer
    {
        public Magazine()
        {
            Issues = new List<Issue>();
        }
        public List<Issue> Issues { get; set; }
    }
    public class Issue : Attributer
    {
        public string MagazineID { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public List<Chapter> Chapters { get; set; }
        public Issue()
        {
            Chapters = new List<Chapter>();
        }
    }
    public class Chapter : Attributer
    {
        public string IssueID { get; set; }
        public List<Article> Articles { get; set; }
        public Chapter()
        {
            Articles = new List<Article>();
        }
    }
    public class Article : Attributer
    {
        public string ChapterID { get; set; }
        public string Status { get; set; }
        public List<Comment> Comments { get; set; }
        public List<Rating> Ratings { get; set; }
        public Article()
        {
            Comments = new List<Comment>();
            Ratings = new List<Rating>();
        }
    }
}
